package com.codes.controller;

import com.codes.core.utils.word.PoiTlUtils;
import com.codes.core.utils.word.WordMergeUtils;
import com.codes.core.utils.word.WordUtils;
import lombok.extern.slf4j.Slf4j;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date 2020/6/23 16:56
 * @Created by peanut
 */

@Slf4j
@RestController
@RequestMapping("/word")
public class ExportCtr  extends  BaseController{


    @GetMapping("/get")
    public void getWord(HttpServletRequest request,
                        HttpServletResponse response) throws IOException {

       File f =                PoiTlUtils.getWord();

       WordUtils.fileToResponse( response,f);

    }
    @GetMapping("/hebing")
    public void getWordHeBing(HttpServletRequest request,
                        HttpServletResponse response) throws IOException, Docx4JException {

        File f =   WordMergeUtils.wordMLToWord(  PoiTlUtils.getWord());
        File f2 =  WordMergeUtils.wordMLToWord(  PoiTlUtils.getWord());
        List<File> fileList = new ArrayList<>(10);
        fileList.add(f);
        fileList.add(f2);
        File re = WordMergeUtils.mergeWordML(fileList,"test998.docx");
        WordUtils.fileToResponse( response,re);

    }








    @RequestMapping("/to-pdf")
    public void docToPdf(HttpServletRequest request,
                         HttpServletResponse response , @RequestParam Map<String,Object> map)   {

        String id = request.getParameter("id");

        try {
            //生成 doc
            File doc = PoiTlUtils.getWord();
            // doc转换为pdf
            File pdf = WordUtils.docToPdf(doc);
            //返回pdf 文件流  给前台
            WordUtils.fileToResponse( response,pdf);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    // end
}